## ----setup, include=TRUE, results='hide', message = FALSE, warning = FALSE, cache=FALSE--------------------------------------------
options(width = 70)
library(data.table)
library(extraoperators)


## ----------------------------------------------------------------------------------------------------------------------------------
options(stringsAsFactors = FALSE,
        datatable.print.nrows = 20, #if over 20 rows
        datatable.print.topn = 3, #print first and last 3
        digits = 2) #reduce digits printed by R

iris$Species <- as.character(iris$Species)
diris <- as.data.table(iris)


diris


## ----------------------------------------------------------------------------------------------------------------------------------
tables()


## ----------------------------------------------------------------------------------------------------------------------------------
sapply(diris, class)


## ----------------------------------------------------------------------------------------------------------------------------------
haskey(diris)
setkey(diris, Species)
key(diris)
haskey(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris[order(Sepal.Length)]
diris
haskey(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris[order(Sepal.Length, -Sepal.Width)]
diris


## ----------------------------------------------------------------------------------------------------------------------------------
setkey(diris, Species)
diris[49:52]


## ----------------------------------------------------------------------------------------------------------------------------------
anyDuplicated(diris)
anyDuplicated(diris$Sepal.Length)


## ----------------------------------------------------------------------------------------------------------------------------------
haskey(diris)
key(diris)
table(duplicated(diris)) # not respecting key
table(duplicated(diris, by = key(diris))) # respecting key


## ----------------------------------------------------------------------------------------------------------------------------------
unique(diris, by = key(diris))


## ----------------------------------------------------------------------------------------------------------------------------------
diris[1:5]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[-(1:148)]


## ----------------------------------------------------------------------------------------------------------------------------------
diris["setosa"]

diris[!"setosa"]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[Sepal.Length < 5]

diris[Sepal.Length < 5 & Petal.Width < .2]

diris[Sepal.Length == 4.3 | Sepal.Length == 4.4]


## ----------------------------------------------------------------------------------------------------------------------------------
interest <- c(4.3, 4.4)

diris[Sepal.Length %in% interest]

diris[Sepal.Length %nin% interest]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, Sepal.Length]
is.data.table(diris[,Sepal.Length])


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, .(Sepal.Length, Sepal.Width)]

diris[, .(Sepal.Length)]


## ----------------------------------------------------------------------------------------------------------------------------------
x <- "example"
x
"x"


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, 1, with = FALSE]

diris[, "Sepal.Length", with = FALSE]


## ----------------------------------------------------------------------------------------------------------------------------------
v <- "Sepal.Length"
diris[, v, with = FALSE]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, v]
diris[, 1]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[1, -"Sepal.Length", with=FALSE]
diris[1, !"Sepal.Length", with=FALSE]
diris[1, -c("Sepal.Length", "Petal.Width"), with=FALSE]
diris[1, -v, with=FALSE]


## ----------------------------------------------------------------------------------------------------------------------------------
head(diris[["Sepal.Length"]])
head(diris[[v]])
head(diris[, Sepal.Length])
head(diris$Sepal.Length)


## ----------------------------------------------------------------------------------------------------------------------------------
names(diris)
colnames(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
setnames(diris, old = "Sepal.Length", new = "SepalLength")
names(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
setnames(diris, old = 1, new = "SepalL")
names(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
setcolorder(diris, c("SepalL", "Petal.Length", "Sepal.Width", "Petal.Width", "Species"))
diris


## ----------------------------------------------------------------------------------------------------------------------------------
v <- c(1, 3, 2, 4, 5)
setcolorder(diris, v)
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- as.data.table(iris)
setkey(diris, Species)


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, V0 := 0]
diris[, Sepal.Length := NULL]
diris[, c("X1", "X2") := .(1L, 2L)]
diris[, V := Petal.Length * Petal.Width]
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, c("V", "V0") := NULL]
diris[1]


## ----------------------------------------------------------------------------------------------------------------------------------
diris["setosa", V := Petal.Length * Petal.Width]
unique(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
diris["virginica", V := sqrt(Petal.Length * Petal.Width)]
unique(diris)


## ----------------------------------------------------------------------------------------------------------------------------------
mean(diris$Sepal.Width)
diris[, mean(Sepal.Width)]
diris[, .(M = mean(Sepal.Width))]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, .(M = mean(Sepal.Width)), by = Species]


diris[, .(M1 = mean(Sepal.Width),
          M2 = mean(Petal.Width)), by = Species]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, .(M1 = mean(Sepal.Width),
          M2 = mean(Petal.Width)), by = Species][,
  .(r = cor(M1, M2))]


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, MedPW := Petal.Width > median(Petal.Width), by = Species]
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris[, .(M1 = mean(Sepal.Width),
          M2 = mean(Petal.Width)),
      by = .(Species, MedPW)]


diris[, .(M1 = mean(Sepal.Width),
          M2 = mean(Petal.Width)),
      by = .(Species, MedPW)][,
  .(r = cor(M1, M2)), by = MedPW]


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris[, .(Sepal.Width = Sepal.Width[1:3]), keyby = Species]
diris2 <- unique(diris)
dalt1 <- data.table(
  Species = c("setosa", "setosa", "versicolor", "versicolor",
              "virginica", "virginica", "other", "other"),
  Type = c("wide", "wide", "wide", "wide",
           "narrow", "narrow", "moderate", "moderate"),
  MedPW = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE))
setkey(dalt1, Species)

dalt2 <- unique(dalt1)[1:2]


diris
diris2
dalt1
dalt2


## ----------------------------------------------------------------------------------------------------------------------------------
merge(diris2, dalt2)


## ----------------------------------------------------------------------------------------------------------------------------------
merge(diris2, dalt2, all.x = TRUE)

merge(diris2, dalt2, all.y = TRUE)

merge(diris2, dalt2, all = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
merge(diris, dalt2)

merge(diris, dalt2, all.y = TRUE)

merge(diris, dalt2, all = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
merge(diris, dalt1, allow.cartesian = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
merge(diris, dalt1, all = TRUE, allow.cartesian = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
d2key1 <- data.table(
  ID1 = c(1, 1, 2, 2),
  ID2 = c(1, 2, 1, 2),
  X = letters[1:4])

d2key2 <- data.table(
  ID1 = c(1, 1, 2, 2),
  ID2 = c(1, 2, 1, 2),
  Y = LETTERS[1:4])

d2key1
d2key2

setkey(d2key1, ID1)
setkey(d2key2, ID1)

merge(d2key1, d2key2)


## ----------------------------------------------------------------------------------------------------------------------------------
setkey(d2key1, ID1, ID2)
setkey(d2key2, ID1, ID2)

merge(d2key1, d2key2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- as.data.table(iris)

diris[, ID := 1:.N]
setkey(diris, ID)


## ----------------------------------------------------------------------------------------------------------------------------------
diris.long <- melt(diris,
  measure.vars = list(
    c("Sepal.Length", "Sepal.Width"),
    c("Petal.Length", "Petal.Width")),
  variable.name = "Type",
  value.name = c("Sepal", "Petal"),
  id.vars = c("ID", "Species"))
diris.long


## ----------------------------------------------------------------------------------------------------------------------------------
diris.long[, Type := factor(Type, levels = 1:2,
                            labels = c("Length", "Width"))]
diris.long


## ----------------------------------------------------------------------------------------------------------------------------------
diris.long2 <- melt(diris, id.vars = c("ID", "Species"))
diris.long2


## ----------------------------------------------------------------------------------------------------------------------------------
dcast(diris.long2, Species ~ variable)


## ----------------------------------------------------------------------------------------------------------------------------------
diris.wide2 <- dcast(diris.long2, ID + Species ~ variable)
diris.wide2


## ----------------------------------------------------------------------------------------------------------------------------------
diris.wide <- dcast(diris.long, ID + Species ~ Type,
      value.var = list("Sepal", "Petal"),
      sep = ".")


all.equal(diris.wide2, diris.wide)

